#include "adc.h"
#define ADC_START_ADDR
u16 adc_read(u16 channel)
{
	sys_read_vp(ADC_START_ADDR+channel,(u8 *)&channel,1);
	return channel;
}

u16 adc_read_avg(u16 channel,u16 avg_cnt)
{
	u16 cnt;
	u16 ad;
	u16 max_ad = 0;
	u16 min_ad = 0xFFFF;
	float ad_sum=0;
	for(cnt=0;cnt <avg_cnt;cnt++)
	{
		ad=adc_read(channel);
		ad_sum+=ad;
		if(ad>max_ad)
			ad=max_ad;
		if(ad<min_ad)
			ad=min_ad;
	}
	if(avg_cnt>2)
	{
		ad_sum = ad_sum-max_ad-min_ad;
		avg_cnt-=2;
	}
	ad=(u16)(ad_sum/avg_cnt+0.5f);
	return ad;
}
